<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    xData: {
      type: Array,
      default: () => {
        return ["Industries", "Technology", "Forex", "Gold", "Forecasts"];
      },
    },
    yData: {
      type: Array,
      default: () => {
        return [
          { value: 320, name: "Industries" },
          { value: 240, name: "Technology" },
          { value: 149, name: "Forex" },
          { value: 100, name: "Gold" },
          { value: 59, name: "Forecasts" },
        ];
      },
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {},
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    handleSize(val, initWidth = 1920) {
      //现在很多浏览器都是1920*1080，initWidth值可根据自己需求进行修改
      let nowClientWidth = document.documentElement.clientWidth;
      return val * (nowClientWidth / initWidth);
    },
    initChart(type) {
      if (this.chart) {
        this.chart.dispose();
        this.chart = null;
      }
      this.chart = echarts.init(this.$el, "macarons");
      this.chart.setOption({
        tooltip: {
          trigger: "item",
        },
        textStyle: {
          color: "#3d3d3d",
        },
        legend: {
          left: "center",
          top: "10px",
          data: this.xData.map((item) => {
            return item.name;
          }),
          textStyle: {
            // color: "#fff",
          },
        },
        // graphic: {
        //   type: "text",
        //   left: "center",
        //   top: "center",
        //   style: {
        //     //text:'用户统计\n'+'100', //使用“+”可以使每行文字居中
        //     text: "总量100", //使用“+”可以使每行文字居中
        //     textAlign: "center",
        //     fill: "#fff",
        //     width: 30,
        //     height: 1,
        //   },
        // },
        series: [
          {
            name: "",
            type: "pie",
            // roseType: "radius",  //玫瑰饼图
            radius: this.handleSize(100),
            center: ["50%", "50%"],
            data: this.yData,
            animationEasing: "cubicInOut",
            animationDuration: 2600,
            label: {
              show: true,
              position: "outside",
              textStyle: {
                fontWeight: 500,
                fontSize: this.handleSize(14),
                color: "#3d3d3d",
              },
              formatter: "{b}: {c}",
            },
            itemStyle: {
              normal:
                type == "alarm"
                  ? {}
                  : {
                      color: function (colors) {
                        var colorList = ["#ed654a", "#fc9a07", "#d88810"];
                        return colorList[colors.dataIndex];
                      },
                    },
            },
          },
        ],
      });
    },
  },
};
</script>
